package com.kzd.btgame.main.mine

import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.custom.setTransitionStatus
import com.kzd.btgame.databinding.ActivityTransferDetailsBinding
import com.kzd.btgame.entity.GameTransfer
import com.kzd.btgame.jetpack.vm.MineViewModel
import com.kzd.btgame.main.KEY_GAME_DETAILS_ID
import com.kzd.btgame.main.game.adapter.GameListLabelAdapter
import com.kzd.btgame.main.toTransferApplyActivity
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadImage
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel
import java.lang.StringBuilder

class TransferDetailsActivity : OwnerViewBindingActivity<ActivityTransferDetailsBinding>() {

    private val mGameId: String
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_ID) ?: "0"

    private val mViewModel: MineViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityTransferDetailsBinding
        get() = ActivityTransferDetailsBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flTransferDetailsBar
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {
        mViewBinding.tvTransferDetailsSubmit.setOnClickAntiShakeListener {
            toTransferApplyActivity(mGameId)
        }
    }

    override fun initData() {
        mViewModel.mineTransferDetails(mGameId)
    }

    override fun initViewMode() {
        mViewModel.transferDetailsLiveData.observe(this as LifecycleOwner, { response ->
            mViewBinding.mslMultiStatusLayout.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })
    }

    private fun fillData(transfer: GameTransfer) {
        mViewBinding.includeViewGame1.ivGame1Icon.loadImage(
            src = transfer.icon ?: R.mipmap.ic_80008,
            R.mipmap.ic_80008,
            R.mipmap.ic_80008
        )
        mViewBinding.includeViewGame1.tvGame1Name.text = transfer.name
        mViewBinding.includeViewGame1.tvGame1Type.text = StringBuilder().append(transfer.type)
            .append("\t·\t").append(transfer.number).append("次下载")
        mViewBinding.includeViewGame1.rvGame1LabelBody.apply {
            layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
        }.adapter = GameListLabelAdapter().apply {
            setNewInstance(transfer.biaoqian.toMutableList())
        }

        mViewBinding.tvTransferDetailsRule.text = transfer.turn_rule
        mViewBinding.tvTransferDetailsMark.text = transfer.turn_remarks
    }
}